<template>
  <el-dialog
    :model-value="modelValue"
    @update:model-value="(value) => emit('update:modelValue', value)"
    title="水库划界地图"
    width="1200px"
    :close-on-click-modal="false"
    top="5vh"
  >
    <div class="boundary-map-container">
      <div class="map-toolbar">
        <el-button-group>
          <el-button :type="currentTool === 'view' ? 'primary' : 'default'" @click="setTool('view')">
            <el-icon><View /></el-icon>
            查看
          </el-button>
          <el-button :type="currentTool === 'draw' ? 'primary' : 'default'" @click="setTool('draw')">
            <el-icon><Edit /></el-icon>
            绘制
          </el-button>
          <el-button :type="currentTool === 'measure' ? 'primary' : 'default'" @click="setTool('measure')">
            <el-icon><Location /></el-icon>
            测量
          </el-button>
        </el-button-group>
        
        <el-button-group style="margin-left: 20px;">
          <el-button @click="zoomIn">
            <el-icon><ZoomIn /></el-icon>
            放大
          </el-button>
          <el-button @click="zoomOut">
            <el-icon><ZoomOut /></el-icon>
            缩小
          </el-button>
          <el-button @click="resetView">
            <el-icon><Refresh /></el-icon>
            重置
          </el-button>
        </el-button-group>

        <div style="margin-left: 20px; display: inline-block;">
          <el-select v-model="selectedLayer" placeholder="选择图层" style="width: 150px;">
            <el-option label="卫星图" value="satellite" />
            <el-option label="地形图" value="terrain" />
            <el-option label="街道图" value="street" />
          </el-select>
        </div>
      </div>

      <div class="map-content">
        <div class="map-area" ref="mapContainer">
          <!-- 地图容器 -->
          <div class="map-placeholder">
            <el-icon size="64"><Location /></el-icon>
            <p>地图加载中...</p>
          </div>
        </div>

        <div class="boundary-info">
          <el-card header="划界信息">
            <el-form :model="boundaryForm" label-width="80px" size="small">
              <el-form-item label="区域名称">
                <el-input v-model="boundaryForm.name" placeholder="请输入区域名称" />
              </el-form-item>
              
              <el-form-item label="区域类型">
                <el-select v-model="boundaryForm.type" placeholder="选择区域类型" style="width: 100%">
                  <el-option label="核心区" value="core" />
                  <el-option label="缓冲区" value="buffer" />
                  <el-option label="实验区" value="experimental" />
                  <el-option label="管理区" value="management" />
                </el-select>
              </el-form-item>

              <el-form-item label="面积">
                <el-input v-model="boundaryForm.area" placeholder="自动计算" readonly>
                  <template #append>平方米</template>
                </el-input>
              </el-form-item>

              <el-form-item label="周长">
                <el-input v-model="boundaryForm.perimeter" placeholder="自动计算" readonly>
                  <template #append>米</template>
                </el-input>
              </el-form-item>

              <el-form-item label="描述">
                <el-input
                  v-model="boundaryForm.description"
                  type="textarea"
                  :rows="3"
                  placeholder="请输入区域描述"
                />
              </el-form-item>
            </el-form>

            <div class="boundary-actions">
              <el-button size="small" @click="clearBoundary">清除</el-button>
              <el-button size="small" type="primary" @click="saveBoundary">保存</el-button>
            </div>
          </el-card>

          <el-card header="图层控制" style="margin-top: 10px;">
            <div class="layer-controls">
              <div class="layer-item" v-for="layer in layers" :key="layer.id">
                <el-checkbox v-model="layer.visible" @change="toggleLayer(layer)">
                  {{ layer.name }}
                </el-checkbox>
                <el-slider
                  v-model="layer.opacity"
                  :min="0"
                  :max="100"
                  size="small"
                  style="margin-left: 10px; width: 80px;"
                  @input="updateLayerOpacity(layer)"
                />
              </div>
            </div>
          </el-card>
        </div>
      </div>
    </div>

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="emit('update:modelValue', false)">关闭</el-button>
        <el-button type="primary" @click="exportMap">导出地图</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted, onUnmounted } from 'vue'
import { ElMessage } from 'element-plus'
import { View, Edit, ZoomIn, ZoomOut, Refresh, Location } from '@element-plus/icons-vue'

interface Props {
  modelValue: boolean
  boundaryData?: any
}

interface Emits {
  (e: 'update:modelValue', value: boolean): void
  (e: 'success'): void
}

const props = withDefaults(defineProps<Props>(), {
  boundaryData: null
})

const emit = defineEmits<Emits>()

const mapContainer = ref()
const currentTool = ref('view')
const selectedLayer = ref('satellite')

const boundaryForm = reactive({
  name: '',
  type: '',
  area: '',
  perimeter: '',
  description: ''
})

const layers = ref([
  { id: 'boundary', name: '划界线', visible: true, opacity: 80 },
  { id: 'markers', name: '标记点', visible: true, opacity: 100 },
  { id: 'grid', name: '网格', visible: false, opacity: 50 }
])

// 设置工具
const setTool = (tool: string) => {
  currentTool.value = tool
  ElMessage.info(`已切换到${tool === 'view' ? '查看' : tool === 'draw' ? '绘制' : '测量'}模式`)
}

// 地图操作
const zoomIn = () => {
  ElMessage.info('放大地图')
}

const zoomOut = () => {
  ElMessage.info('缩小地图')
}

const resetView = () => {
  ElMessage.info('重置视图')
}

// 图层控制
const toggleLayer = (layer: any) => {
  ElMessage.info(`${layer.visible ? '显示' : '隐藏'}图层: ${layer.name}`)
}

const updateLayerOpacity = (layer: any) => {
  console.log(`更新图层透明度: ${layer.name} - ${layer.opacity}%`)
}

// 划界操作
const clearBoundary = () => {
  Object.assign(boundaryForm, {
    name: '',
    type: '',
    area: '',
    perimeter: '',
    description: ''
  })
  ElMessage.success('已清除划界')
}

const saveBoundary = () => {
  if (!boundaryForm.name || !boundaryForm.type) {
    ElMessage.warning('请填写区域名称和类型')
    return
  }
  
  ElMessage.success('划界保存成功')
  emit('success')
}

// 导出地图
const exportMap = () => {
  ElMessage.success('地图导出成功')
}

// 初始化地图
const initMap = () => {
  // 这里应该初始化实际的地图组件
  console.log('初始化地图')
}

onMounted(() => {
  initMap()
})

onUnmounted(() => {
  // 清理地图资源
})
</script>

<style scoped>
.boundary-map-container {
  height: 70vh;
  display: flex;
  flex-direction: column;
}

.map-toolbar {
  padding: 10px 0;
  border-bottom: 1px solid #e4e7ed;
  margin-bottom: 10px;
}

.map-content {
  flex: 1;
  display: flex;
  gap: 10px;
}

.map-area {
  flex: 1;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  position: relative;
  background: #f5f7fa;
}

.map-placeholder {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  text-align: center;
  color: #909399;
}

.boundary-info {
  width: 300px;
  display: flex;
  flex-direction: column;
}

.boundary-actions {
  margin-top: 15px;
  text-align: right;
}

.layer-controls {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.layer-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.dialog-footer {
  text-align: right;
}
</style>